<template>
  <div class="dashboard-container">
    <div class="app-container">
      
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hNew"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index"/>
              <el-table-column label="角色名称" width="240" prop="name"/>
              <el-table-column label="描述" prop="description"/>
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDele(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              
              <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="paging.page"
      :page-sizes="[1, 3, 5, 99]"
      :page-size="paging.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

            </el-row>
          </el-tab-pane>

          <!-- 新增弹框 -->
<el-dialog title="编辑弹层"  @close="hreset"
   :visible.sync="showDialog">
  <el-form ref="roleForm" :model="formList" :rules="rules" label-width="100px">
    <el-form-item label="角色名称" prop="name">
      <el-input v-model="formList.name" />
    </el-form-item>
    <el-form-item label="角色描述" prop="description">
      <el-input v-model="formList.description" />
    </el-form-item>
  </el-form>
  <!-- 底部 -->
  <el-row slot="footer" type="flex" justify="center">
    <el-col :span="6">
      <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hQu">取消</el-button>
    </el-col>
  </el-row>
</el-dialog>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getSettings ,addSettings,editSettings ,deleSettings} from "@/api/settings";
export default {
  data() {
    return {
      // 分页 
      paging:{
        page:1,//页码
        pagesize:3//每页条数
      },
      // 员工信息数据 
      list:[],
      sit:false,
      // 收集数据
      formList:{
        name:'',
        description:''
      },
      // 校验 
      rules:{
        name:[
           { required: true, message: '请输入角色名称', trigger: 'blur' },
        ],
        description:[
           { required: true, message: '请输入角色描述', trigger: 'blur' },
        ]
      },
      // 总条数
    total:0,
    // 显示隐藏
    showDialog:false,
    };
    
  },
  created(){
    this.loadGetSettings()
  },
  methods:{
    // 重置按钮
    hreset(){
      
      this.$refs.roleForm.resetFields()
      this. formList={
        name:'',
        description:''
      }
    },
    // 取消
    hQu(){
      this.showDialog=false
    },
    // 删除角色
    hDele(id){
    this.$confirm('是否确定删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          try {
         await deleSettings(id)
        if(this.list.length<=1){
          this.paging.page--
          this.loadGetSettings()
          if(this.paging.page===0){
            this.paging.page=1
            this.loadGetSettings()
          }
        }
        this.loadGetSettings()
      } catch (error) {
        console.log(error);
      }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });

      
    },

    // 编辑角色
   async loadEditSettings(){
    try {
      const res = await editSettings(this.formList)
     
      this.loadGetSettings()
      // console.log(res);
    } catch (error) {
      console.log(error);
    }
   },
    // 编辑
    hEdit(data){
      this.showDialog=true
      this.sit=true
      this.formList={...data}
      
    },
    // 确认按钮
    hSubmit(){
      this.$refs.roleForm.validate((valid)=>{
        if(!valid) return
        this.sit?this.loadEditSettings():this.loadAddSettings()
      })
      this.showDialog=false
    },
    // 添加角色
   async loadAddSettings(){
    const  res = await addSettings(this.formList)
    // console.log(res);
     this.total++
    const kpi = Math.ceil(this.total/this.paging.pagesize)
    this.paging.page=kpi
    this.loadGetSettings()
   },
    // 新增按钮
    hNew(){
      this.showDialog=true
      this.sit=false
    },
    // 分页
     handleSizeChange(val) {
        // console.log(`每页 ${val} 条`);
        this.paging.page=1
        this.paging.pagesize=val
        this.loadGetSettings()
      },
      handleCurrentChange(val) {
        // console.log(`当前页: ${val}`);
        this.paging.page=val
        this.loadGetSettings()
      },
      // 获取全部员工信息
    async  loadGetSettings(){
      try {
        const  res = await getSettings(this.paging)
        // console.log(res);
        this.list = res.data.rows
        this.total=res.data.total
      } catch (error) {
        console.log(error);
      }
    }
  }
}
</script>

<style>

</style>